﻿@{
    ViewBag.Title = "Quản lý chuyên mục rao vặt";
    Layout = "~/Areas/Admin/Views/Shared/_LayoutPageAdmin.cshtml";
}

<div class="window_add_object"></div>
<div class="window_update_object"></div>

<h2 class="title_h2">Quản lý chuyên mục rao vặt</h2>

<div class="search_Cate">
                <label class="category-label" for="category">Show by category:</label>
               <input type="search" id="category_object" style="width: 250px"/>
</div>


<div id="grid_object"></div>
<div class="k-toolbar k-grid-toolbar k-widget">
        <div class="toolbar toorbar_footer">
            <a class="k-button k-button-icontext k-button-custom" onclick="return Add_object();" id="Add_object" href="javascript:void(0)">
                <span class="k-icon k-add"></span>
                Thêm chuyên mục</a>

            <a class="k-button k-button-icontext k-button-custom" onclick="return Update_Object();" id="Update_Object" href="javascript:void(0)">
                <span class="k-icon k-add"></span>
                Sửa chuyên mục</a>

            <a class="k-button k-button-icontext k-button-custom" onclick="return Delete_Khach_hang();" id="delete_linh_kien" href="javascript:void(0)">
                <span class="k-icon k-add"></span>
                Xóa chuyên mục</a>
        </div>
        
    </div>
<script type="text/javascript">
    $(document).ready(function () {
        var crudServiceBaseUrl = "/Admin/Ad_Cate",
            dataSource = new kendo.data.DataSource({
                transport: {
                    read: {
                        url: crudServiceBaseUrl + "/_List",
                        dataType: "json"
                    },
                    parameterMap: function (options, operation) {
                        //var output = null;
                        //if (operation !== "read" && options.models) {
                        //    output = '{ filters: ' + options.models + '}';
                        //}
                        //return kendo.stringify(output);
                        if (operation == "update" && options.models)
                        {
                            return { models: options.models };

                        } else
                        if (operation !== "read" && options.models) {
                            return { models: options.models };
                        }
                    }
                },
                batch: true,
                //pageSize: 25,
                schema: {
                    model: {
                        id: "Id",
                        fields: {
                            Id: { editable: false, nullable: true },
                            ParentId: { editable: false, nullable: true },
                            TenChuyenMuc: { validation: { required: true } },
                            Meta_Title: { validation: { required: true } },
                            Hot: { type: "boolean" },
                            set_footer: { type: "boolean" },
                            stt: { type: "number", validation: { required: true, min: 1} },
                            Icon: { validation: { required: false } },
                            Ma_QG: { validation: { required: false } },
                            Meta_KeyWork: { validation: { required: false } }
                        }
                    }
                }
            });

        $("#grid_object").kendoGrid({
            dataSource: dataSource,
            pageable: true,
            selectable: "single",
            height: $(document).height() - 175,
            scrollable: true,
            sortable: {
                mode: "single",
                allowUnsort: false
            },
            //toolbar: ["create"],
            columns: [
                { field: "TenChuyenMuc", title: "Tên Chuyên mục" },
                { field: "ParentId", title: "Chuyên mục cha", hidden: true },
                { field: "Hot", title: "Đặc biệt", width: "80px" },
                { field: "set_footer", title: "Footer", width: "80px" },
                { field: "Icon", title: "Icon", hidden : true },
                { field: "stt", title: "Số TT", width: "80px" },
                { field: "Meta_Title", title: "Meta Title", hidden: true },
                { field: "Meta_Des", title: "Meta Description", hidden: true },
                { field: "Meta_KeyWork", title: "Key Work" }],
            editable: { mode: "popup", confirmation: false }
        });

        $('#grid_object table tr').live('dblclick', function (e) {
            var gridDS_hang_hoa_up_click = $("#grid_object").data("kendoGrid");
            var select_hh_update = gridDS_hang_hoa_up_click.select();
            var data_select_hang_hoa_upate = gridDS_hang_hoa_up_click.dataItem(select_hh_update);
            var value_malk = data_select_hang_hoa_upate.Id;
            loadwindowOnContent("/Admin/Ad_Cate/Update?id=" + value_malk, "window_update_object", "Cập nhật thông tin chuyên mục", 600, 500);
        });

        $(window).resize(function () {
            resizeGrid_Object();
        });

        var dropDown = $("#category_object").kendoDropDownList({
            dataTextField: "TenChuyenMuc",
            dataValueField: "Id",
            autoBind: false,
            optionLabel: "All",
            dataSource: {
                type: "json",
                severFiltering: true,
                transport: {
                    read: crudServiceBaseUrl + "/getCateParent",
                }
            },
            change: function () {
                var value = this.value();
                if (value) {
                    $("#grid_object").data("kendoGrid").dataSource.filter({ field: "ParentId", operator: "eq", value: parseInt(value) });
                } else {
                    $("#grid_object").data("kendoGrid").dataSource.filter({});
                }
            }
        });

    });

    function resizeGrid_Object() {
        var gridElement = $("#grid_object");
        var dataArea = gridElement.find(".k-grid-content");

        var newGridHeight = $(document).height() - 175;
        //var newDataAreaHeight = newGridHeight - 65;

        //dataArea.height(newDataAreaHeight);
        gridElement.height(newGridHeight);

        $("#grid_object").data("kendoGrid").refresh();
    }

    function Add_object() {
        loadwindowOnContent("/Admin/Ad_Cate/Add", "window_add_object", "Thêm thông tin chuyên mục", 600, 425);
    }

    function Update_Object() {
        var gridDS_hang_hoa_up_click = $("#grid_object").data("kendoGrid");
        var select_hh_update = gridDS_hang_hoa_up_click.select();

        if (select_hh_update.length == 0) {
            $.confirm({
                text: "Vui lòng nhấp chọn một chuyên mục để cập nhật",
                confirm: function (button) {
                },
                cancel: function (button) {

                }
            });
        }
        else {
            var data_select_hang_hoa_upate = gridDS_hang_hoa_up_click.dataItem(select_hh_update);
            var value_malk = data_select_hang_hoa_upate.Id;
            loadwindowOnContent("/Admin/Ad_Cate/Update?id=" + value_malk, "window_update_object", "Cập nhật thông tin chuyên mục", 600, 500);
        }

    }

</script>

